package com.aivin.lib_base.tool;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.res.Resources;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import java.io.File;

public class MyDeviceInforHelper {
	private static WindowManager myWindowManager ;
	private static DisplayMetrics myDisplayMetrics ;

	private static WindowManager myGetWindowManageger(Context context) {
		if(myWindowManager==null){
			myWindowManager = (WindowManager) (context.getSystemService(Context.WINDOW_SERVICE));
		}
		return myWindowManager;
	}

	private static DisplayMetrics myGetDisplayMetrics( ) {
		if(myDisplayMetrics==null){
			myDisplayMetrics = new DisplayMetrics();
		}
		return myDisplayMetrics;
	}


	/**
	 * 适合在自定义控件中调用
	 */
	public static int getWindowHeigh(Context context) {
		myGetWindowManageger(context).getDefaultDisplay().getMetrics(myGetDisplayMetrics());
		return myGetDisplayMetrics().heightPixels;
	}

	/**
	 * 适合在自定义控件中调用
	 */
	public static int getWindowWidth(Context context) {
		myGetWindowManageger(context).getDefaultDisplay().getMetrics(myGetDisplayMetrics());
		return  myGetDisplayMetrics().widthPixels;
	}

	/**
	 * 获取屏幕宽度
	 */
	public static int getScreenWidth(Context context) {
		WindowManager wm = (WindowManager) context   .getSystemService(Context.WINDOW_SERVICE);
		return wm.getDefaultDisplay().getWidth();
	}

	/**
	 *  获取虚拟导航栏 高度
	 */
	public static int getNavigationBarHeight(Activity mActivity) {
		Resources resources = mActivity.getResources();
		int resourceId=resources.getIdentifier("navigation_bar_height","dimen","android");
		return resources.getDimensionPixelSize(resourceId);

	}

	public static int getStatusBarHeight(Activity mActivity) {
		Resources resources = mActivity.getResources();
		int resourceId = resources.getIdentifier("status_bar_height","dimen","android");
		return resources.getDimensionPixelSize(resourceId);
	}


	public static String getEnglishDir(  ) {
		String  localDir =  getSdRootDir()+  "/aivin/english/";
		File file = new File(localDir);
		if (!file.exists())  {
			boolean reslut =file.mkdirs();
			if(!reslut)  {
				return null ;
			}
		}
		return  localDir ;
	}

	private static String getSdRootDir( ) {
		return  Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)?
				Environment.getExternalStorageDirectory() .getAbsolutePath():
				Environment.getDataDirectory().getAbsolutePath() ;
	}


	public static String getAppVersion(Context context) {
		PackageInfo pacageInfo ;
		String versionName ;
		try {
			pacageInfo = context.getPackageManager().getPackageInfo( context.getPackageName(), 0);
			versionName = pacageInfo.versionName;
		} catch ( Exception e) {
			versionName="excpetion" ;
		}
		return versionName;
	}

	public static int dp2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}


}
